package br.com.caelum.lojavirtual.dao;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Session;

public class Dao<T,Id extends Serializable> {

	private Session session;
	@SuppressWarnings("unchecked")
	private final Class classe;
	
	@SuppressWarnings("unchecked")
	public Dao(Session session, Class classe) {
		this.session = session;
		this.classe = classe;
	}
	public void adiciona(T u){
		this.session.save(u);
	}
	public void remove(T u){
		this.session.delete(u);
	}
	public void atualiza(T u){
		this.session.update(u);
	}
	@SuppressWarnings("unchecked")
	public List<T> listaTudo(){
		return this.session.createCriteria(this.classe).list();
	}
	@SuppressWarnings("unchecked")
	public T procura(Id id){
		return (T) this.session.load(this.classe, id);
	}
	protected Session getSession() {
		return session;
	}
}
